package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ContractProjectRelation;
import com.ruoyi.system.service.IContractManageService;
import com.ruoyi.system.service.IContractProjectRelationService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ContractProjectMapper;
import com.ruoyi.system.domain.ContractProject;
import com.ruoyi.system.service.IContractProjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-03-12
 */
@Service
public class ContractProjectServiceImpl implements IContractProjectService
{
    @Autowired
    private ContractProjectMapper contractProjectMapper;
    @Autowired
    private IContractProjectRelationService iContractProjectRelationService;

    @Autowired
    private IContractManageService iContractManageService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public ContractProject selectContractProjectCountById(Long id)
    {
        return contractProjectMapper.selectContractProjectCountById(id);
    }
    @Override
    public ContractProject selectContractProjectById(Long id)
    {
        ContractProject contractProject = contractProjectMapper.selectContractProjectById(id);
        List<ContractProjectRelation> contractProjectRelations = iContractProjectRelationService.selectContractProjectRelationByProjectId(id);
        if(CollectionUtils.isNotEmpty(contractProjectRelations)){
            List<Long> ids = contractProjectRelations.stream().map(e -> e.getContractId()).collect(Collectors.toList());
            contractProject.setRelationIds(ids);
        }
        String fileData = contractProject.getFileData();
        //附件格式处理
        if(StringUtils.isNotEmpty(fileData)){
            String[] split = StringUtils.split(fileData,",");
            List<Map<String,String>> list = new ArrayList<>();
            Map<String,String> fileMap = null;
            for (String s : split) {
                fileMap = new HashMap<>();
                String name = s.substring(s.lastIndexOf("/")+1,s.length());
                fileMap.put("name",name);
                fileMap.put("url",s);
                list.add(fileMap);
            }
            contractProject.setFileList(list);
            contractProject.setFileNameList(Arrays.asList(split));
        }
        return contractProject;
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param contractProject 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<ContractProject> selectContractProjectList(ContractProject contractProject)
    {
        return contractProjectMapper.selectContractProjectList(contractProject);
    }
    @Override
    public List<ContractProject> selectContractProjectcCountDataList(ContractProject contractProject)
    {
        return contractProjectMapper.selectContractProjectcCountDataList(contractProject);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param contractProject 【请填写功能名称】
     * @return 结果
     */
    @Transactional
    @Override
    public int insertContractProject(ContractProject contractProject)
    {
        setFile(contractProject);
        contractProject.setCreateTime(DateUtils.getNowDate());
        contractProjectMapper.insertContractProject(contractProject);
        List<Long> relationIds = contractProject.getRelationIds();
        if(CollectionUtils.isNotEmpty(relationIds)){
            for (Long relationId : relationIds) {
                ContractProjectRelation relation = new ContractProjectRelation();
                relation.setContractId(relationId);
                relation.setProjectId(contractProject.getId());
                iContractProjectRelationService.insertContractProjectRelation(relation);
            }
        }
        return 1;
    }

    private void setFile(ContractProject contractProject) {
        List<String> fileNameList = contractProject.getFileNameList();
        if(CollectionUtils.isNotEmpty(fileNameList)){
            StringJoiner joiner = new StringJoiner(",");
            for (String s : fileNameList) {
                joiner.add(s);
            }
            contractProject.setFileData(joiner.toString());
        }else{
            contractProject.setFileData("");
        }
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param contractProject 【请填写功能名称】
     * @return 结果
     */
    @Transactional
    @Override
    public int updateContractProject(ContractProject contractProject)
    {

        List<Long> relationIds = contractProject.getRelationIds();
        if(CollectionUtils.isNotEmpty(relationIds)){
            iContractProjectRelationService.deleteContractProjectRelationByProjectId(contractProject.getId());
            for (Long relationId : relationIds) {
                ContractProjectRelation relation = new ContractProjectRelation();
                relation.setContractId(relationId);
                relation.setProjectId(contractProject.getId());
                iContractProjectRelationService.insertContractProjectRelation(relation);
            }
        }
        setFile(contractProject);
        contractProject.setUpdateTime(DateUtils.getNowDate());
        return contractProjectMapper.updateContractProject(contractProject);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteContractProjectByIds(Long[] ids)
    {
        return contractProjectMapper.deleteContractProjectByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteContractProjectById(Long id)
    {
        return contractProjectMapper.deleteContractProjectById(id);
    }
}
